#include "lib/trie.h"

Trie::Trie() {
  root = new TrieNode();
}

void Trie::insert(string str) {
  auto node = root;
  for (auto ele : str) {
    if (node->childern[ele - 'a'] == nullptr)
      node->childern[ele - 'a'] = new TrieNode();
    node = node->childern[ele - 'a'];
  }
  node->terminal = true;
}

bool Trie::search(string str) {
  auto node = root;
  for (auto ele : str) {
    if (node->childern[ele - 'a'] == nullptr) {
      return false;
    }
    node = node->childern[ele - 'a'];
  }
  return node->terminal;
}

bool Trie::startWith(string str) {
  auto node = root;
  for (auto ele : str) {
    if (node->childern[ele - 'a'] == nullptr) {
      return false;
    }
    node = node->childern[ele - 'a'];
  }
  return true;
}
